#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
import tornado.web
import tornado.ioloop


class LoginHandler(tornado.web.RequestHandler):
    def data_received(self, chunk):
        pass

    def post(self):
        ret_dic = {'code': 200, 'message': 'ok'}
        post_data = self.request.body.decode('utf-8')
        post_args = json.loads(post_data)
        user_name = post_args.get('useName')
        password = post_args.get('password')
        sql = '''select id, phone, email, gender, birthday from user 
            where userName='{0}' and password='{1}' '''.format(user_name, password)
        results = self.application.db.query(sql)
        if len(results) == 0:
            ret_dic['code'] = 201
            ret_dic['message'] = 'login user {0} not found'.format(user_name)
        else:
            user_id, phone, email, gender, birthday = results[0][:]
            ret_dic['data'] = {
                'userId': user_id,
                'userName': user_name,
                'phone': phone,
                'email': email,
                'gender': gender,
                'birthday': birthday
            }
        self.write(json.dumps(ret_dic))


class RegistHandler(tornado.web.RequestHandler):
    def data_received(self, chunk):
        pass

    def post(self):
        ret_dic = {'code': 200, 'message': 'ok'}
        post_data = self.request.body.decode('utf-8')
        post_args = json.loads(post_data)
        user_name = post_args.get('useName')
        password = post_args.get('password')
        phone = post_args.get('phone')
        email = post_args.get('email')
        gender = post_args.get('gender')
        birthday = post_args.get('birthday')
        ext = post_args.get('ext')

        sql = '''select id, phone, email, gender, birthday from user 
                    where userName='{0}' '''.format(user_name)
        results = self.application.db.query(sql)
        if len(results) != 0:
            ret_dic['code'] = 202
            ret_dic['message'] = 'user {0} has been exists'.format(user_name)
        else:
            sql = '''insert into user(userName, password, phone, email, gender, birthday, ext) 
                    values('{0}', '{1}', '{2}', '{3}', {4}, {5}, '{6}'})
                '''.format(user_name, password, phone, email, gender, birthday, ext)
            self.application.db.excute(sql)
            self.application.db.commit()
        self.write(json.dumps(ret_dic))


class UserHandler(tornado.web.RequestHandler):
    def data_received(self, chunk):
        pass

    def post(self):
        ret_dic = {'code': 200, 'message': 'ok'}
        post_data = self.request.body.decode('utf-8')
        post_args = json.loads(post_data)
        user_name = post_args.get('useName')
        sql = f'''select id, phone, email, gender, birthday from user where userName='{user_name}';'''
        results = self.application.db.query(sql)
        if len(results) == 0:
            ret_dic['code'] = 201
            ret_dic['message'] = 'query user {0} not found'.format(user_name)
        else:
            user_id, phone, email, gender, birthday = results[0][:]
            ret_dic['data'] = {
                'userId': user_id,
                'userName': user_name,
                'phone': phone,
                'email': email,
                'gender': gender,
                'birthday': birthday
            }
        self.write(json.dumps(ret_dic))
